var bg1 = $(".bg1")
var bg2 = $(".bg2")
var point = 0
var timebg = setInterval(function() {
	bg1[0].style.top = bg1[0].offsetTop + 1 + 'px'
	bg2[0].style.top = bg2[0].offsetTop + 1 + 'px'
	if (bg1[0].offsetTop >= 768) {
		bg1[0].style.top = '-768px'
	}
	if (bg2[0].offsetTop >= 768) {
		bg2[0].style.top = '-768px'
	}
}, 10)
var plane = $(".plane")
var all = $(".box")
// pc
plane[0].onmousedown = function(res) {
	var e1 = res || window.event
	basex = e1.pageX
	basey = e1.pageY
	movex = 0
	movey = 0
	all[0].onmousemove = function(res) {
		var e2 = res || window.event
		movex = e2.pageX - basex
		basex = e2.pageX
		movey = e2.pageY - basey
		basey = e2.pageY
		plane[0].style.left = plane[0].offsetLeft + movex + 'px'
		plane[0].style.top = plane[0].offsetTop + movey + 'px'
	}
}
document.onmouseup = function() {
	all[0].onmousemove = null
}


var tiemshoot = setInterval(function() {
	var shootbox = document.createElement('div')
	shootbox.className = 'shoot'
	shootbox.style.left = plane[0].offsetLeft + 39 + 'px'
	shootbox.style.top = plane[0].offsetTop - 15 + 'px'
	all[0].appendChild(shootbox)
	var timeshoot1 = setInterval(function() {
		shootbox.style.top = shootbox.offsetTop - 5 + 'px'
		if (shootbox.offsetTop <= -16) {
			clearInterval(timeshoot1)
			all[0].removeChild(shootbox)
		}
	}, 10)
}, 100)
var tiembad = setInterval(function() {
	var badbox = document.createElement('div')
	badbox.className = 'bad'
	badbox.style.left = parseInt(Math.random()*422) + 'px'
	badbox.style.top = 0
	all[0].appendChild(badbox)
	var timebad1 = setInterval(function() {
		badbox.style.top = badbox.offsetTop + 5 + 'px'
		if (badbox.offsetTop >= 768) {
			clearInterval(timebad1)
			all[0].removeChild(badbox)
		}
	}, 10)
}, 1000)
var timecheck = setInterval(function() {
	var bad = document.getElementsByClassName('bad')
	var shoot = document.getElementsByClassName('shoot')
	for (var i = 0; i < bad.length; i++) {
		for (var j = 0; j < shoot.length; j++) {
			if (check(plane[0],bad[i])) {
				all[0].removeChild(bad[i])
				alert('游戏失败！')
				window.location.reload()
			}
			if (check(shoot[j],bad[i])) {
				all[0].removeChild(bad[i])
				all[0].removeChild(shoot[j])
				point = point +1
				$("#point").text("得分："+point)
			}
		}
	}
}, 10)

function check(b1, b2) {
	var b1w = parseInt(getComputedStyle(b1, null).width)
	var b1h = parseInt(getComputedStyle(b1, null).height)
	var b2w = parseInt(getComputedStyle(b2, null).width)
	var b2h = parseInt(getComputedStyle(b2, null).height)
	var b1x = parseInt(b1.style.left) + b1w / 2
	var b1y = parseInt(b1.style.top) + b1h / 2
	var b2x = parseInt(b2.style.left) + b2w / 2
	var b2y = parseInt(b2.style.top) + b2h / 2
	var x = Math.abs(b1x - b2x)
	var y = Math.abs(b1y - b2y)
	if (x <= ((b1w + b2w) / 2) && y <= ((b1h + b2h) / 2)) {
		return true
	} else {
		return false
	}
}
